package sort.algorithm;

import sort.record.Record;
import sort.record.Records;

/**
 * @author masaru-gotou
 *
 */
public class InsertionSort extends Sorter {

	/* (�� Javadoc)
	 * @see sort.Sorter#sort(sort.Record[])
	 */
	@Deprecated
	public Record[] sort(Record[] record) {
		for (int i = 1; i < record.length; i++) {
			Record tmp = record[i];
			int j;
			for (j = i; j > 0 && record[j-1].getKey() > tmp.getKey(); j--) {
				record[j] = record[j-1];
			}
			record[j] = tmp;
		}
		return record;
	}

	public Records sort(Records records) {
		for (int i = 1; i < records.size(); i++) {
			Record tmp = new Record(records.get(i));
			int j;
			for (j = i; j > 0 && records.get(j-1).getKey() > tmp.getKey(); j--) {
				records.set(j, records.get(j-1));
				setChanged();
			}
			records.set(j, tmp);
			notifyObservers(records);
		}
		return records;
	}

	@Override
	public String getName() {
		return "Insertion Sort";
	}

}
